package co.baiku.boot.web.controller;

import co.baiku.boot.common.message.Message;
import co.baiku.boot.common.message.PageData;
import co.baiku.boot.core.orm.bin.RunSQL;
import co.baiku.boot.core.orm.request.PagingRequest;
import co.baiku.boot.core.web.controller.BaseController;
import co.baiku.boot.orm.dao.StudentDAO;
import co.baiku.boot.orm.po.Student;
import co.baiku.boot.orm.service.StudentService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Student")
public class StudentController extends BaseController {

    @Resource
    private StudentDAO studentDAO;
    @Resource
    private StudentService studentService;
    @Resource
    private RunSQL runSQL;

    @GetMapping("/all")
    public Message all() {
        List<Student> findAll = studentDAO.findAll();
        return Message.result(findAll);
    }

    @GetMapping("/findByStudentNo/{studentNo}")
    public Message all(@PathVariable String studentNo) {
        Student findOne = studentDAO.findFirstByStudentNo(studentNo);
        return Message.result(findOne);
    }

    @GetMapping("/find")
    public Message find(BigDecimal amount) {
        List<Student> students = studentDAO.findByAmountGreaterThanEqual(amount);
        return Message.result(students);
    }

    @GetMapping("/findByAmount")
    public Message findByAmount(BigDecimal amount) {
        List<Student> students = studentDAO.findByAmount(amount);
        return Message.result(students);
    }

    @GetMapping("/findByAmountSQL")
    public Message findByAmountSQL(BigDecimal amount) {
        Map<String, Object> params = new HashMap<>();
        params.put("amount", amount);
        List<Student> students = runSQL.executeSQL("select * from ajavaer_student s where s.amount>=:amount", params, Student.class);
        return Message.result(students);
    }

    @GetMapping("/findByAmountTemplate")
    public Message findByAmountTemplate(BigDecimal amount) {
        Map<String, Object> params = new HashMap<>();
        params.put("amount", amount);
        PageData<Student> findByAmountTemplate = studentDAO.templateBuilder("findByAmountTemplate").addParams(params).query(1, 10, Student.class);
//        List<Student> students = studentDAO.templateQuery("findByAmountTemplate", params, Student.class);
        return Message.result(findByAmountTemplate);
    }

    @PostMapping("/page")
    public Message page(@RequestBody PagingRequest request) {
        return Message.result(studentService.findDtoPaging(request));
    }

    @PostMapping("/dto/page")
    public Message dtoPage(@RequestBody PagingRequest request) {
        return Message.result(studentService.findDtoPaging(request));
    }

    @GetMapping("/getDto/{id}")
    public Message dtoPage(@PathVariable Long id) {
        return Message.result(studentService.getDtoById(id));
    }

    @GetMapping("/get/{id}")
    public Message get(@PathVariable Long id) {
        return Message.result(studentService.getById(id));
    }

    @GetMapping("/delete/{id}")
    public Message delete(@PathVariable Long id) {
        studentService.delete(id);
        return Message.success();
    }
}
